package com.hotcode;

import com.bean.ListNode;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/25 10:52
 */
public class leecode19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode n1 = findN(dummy, n + 1);
        n1.next = n1.next.next;
        return dummy.next;
    }


    public ListNode findN(ListNode head, int n) {
        ListNode p1 = head;
        for (int i = 0; i < n; i++) {
            p1 = p1.next;
        }

        ListNode p2 = head;
        while (p1 != null) {
            p2 = p2.next;
            p1 = p1.next;
        }
        return p2;
    }
}
